package com.baking.controller;

import com.baking.model.Menu;
import com.baking.model.User;
import com.baking.service.IMenuService;
import com.baking.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author:wrb
 * @Date :2020/1/1/16:46
 * @Describtion:
 **/
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    IMenuService iMenuService;

    @RequestMapping("/getMenuList")
    @ResponseBody
    public Msg getMenuList(HttpServletRequest request, HttpServletResponse response){
        Msg msg = new Msg();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user==null){
            msg.doError("重新登录");
            return msg;
        }
        System.out.println(user);
        //该角色拥有的菜单
        String menuList = iMenuService.getMenuList(user.getRoleId());
        if(menuList!=null){
            msg.deSuccess(menuList);
        }else {
            msg.doError("该用户未分配角色！");
        }
        return msg;
    }

    @RequestMapping("/getMenuData")
    public Msg getMenuData(){
        Msg msg = new Msg();
        List<Menu> menuList = iMenuService.getMenuPanel();
        msg.setPageData(menuList);
        return msg;
    }
    @RequestMapping("/index")
    public String toMenuIndex(){
        System.out.println("来到菜单");
        return "/menu/index";
    }
}
